package admin

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"duoke/utils/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var CourseVideo = new(courseVideoCtl)

type courseVideoCtl struct{}

func (ctl *courseVideoCtl) List(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	list, total, err := service.CourseVideo.Init().List(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", gin.H{"total": total, "list": list})
	return
}
func (ctl *courseVideoCtl) Save(c *gin.Context) {
	var req model.CourseVideoTab
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	save, err := service.CourseVideo.Init().Save(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "保存成功", save)
	return
}
func (ctl *courseVideoCtl) Del(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	del, err := service.CourseVideo.Init().Del(id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "删除成功", del)
}
